package com.leetcode.LC;

import java.util.HashMap;
import java.util.Map;

public class LC697 {
    public int findShortestSubArray(int[] nums) {

        int max = 1, min = Integer.MAX_VALUE;
        Map<Integer, Integer[]> map = new HashMap<>();//数组记录出现最大次数和起始位置和终止位置
        for (int i = 0; i < nums.length; i++) {
            if (map.containsKey(nums[i])) {
                Integer[] integers = map.get(nums[i]);
                integers[0]++;
                integers[2] = i;
                map.put(nums[i], integers);
                if (integers[0]>max){
                    max=integers[0];
                }
            } else {
                Integer[] integers = new Integer[3];
                integers[0] = 1;
                integers[1] = i;
                integers[2] = i;
                map.put(nums[i], integers);
            }
        }

        for (Integer[] value : map.values()) {
            if (value[0] == max) {
                min = Math.min(min,value[2] - value[1] + 1);
            }
        }
        System.out.println(max);

        return min;
    }

    public static void main(String[] args) {
        System.out.println(new LC697().findShortestSubArray(new int[]{1}));
    }
}
